<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreServiceRequest;
use App\Http\Requests\UpdateServiceRequest;
use App\Http\Resources\ServiceResource;
use App\Models\Service;
use Illuminate\Http\Request;

class ServiceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $title = $request->title;
        $per_page = $request->per_page ?? 10;
        // [
        //  {"field":"orderby","order":"descend"},
        //  {"field":"readnum","order":"descend"}
        // ]
        $sort = json_decode($request->sort, true);
        logger()->info($sort);

        $resource = Service::select('id', 'title', 'description', 'image', 'orderby', 'hide', 'readnum', 'published_at')
            ->when($title, function ($query) use ($title) {
                return $query->where('title', 'like', '%' . $title . '%');
            })
            ->when($sort, function ($query) use ($sort) {
                foreach ($sort as $item) {
                    $val = str_replace('end', '', $item['order']);
                    $query->orderBy($item['field'], $val);
                }
            }, function ($query) {
                return $query->orderBy('id', 'desc');
            })
            ->paginate($per_page);
        return new ServiceResource($resource);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\StoreServiceRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreServiceRequest $request)
    {
        return $this->successful(Service::create($request->all()));
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Service  $service
     * @return \Illuminate\Http\Response
     */
    public function show(Service $service)
    {
        return $this->successful($service);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Http\Requests\UpdateServiceRequest  $request
     * @param  \App\Models\Service  $service
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateServiceRequest $request, Service $service)
    {
        return $this->successful($service->update($request->all()));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Service  $service
     * @return \Illuminate\Http\Response
     */
    public function destroy(Service $service)
    {
        $service->delete();
        return $this->successful();
    }

    /**
     * Delete many resources
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function deleteMany(Request $request)
    {
        $ids = $request->ids;
        if (empty($ids)) {
            return $this->fail(400, 'ids is required');
        }
        Service::whereIn('id', $ids)->delete();
        return $this->successful($ids);
    }
}
